function isLogin(){
	$.post("/CuitShopPlatform/servlet/isLoginController",{"action":"isLogin"}, function(data){
		if(data.loginflag=='0'){
			return false;
		}
		$(".rightTopBar").empty();
		var node="<a class='userMsg' href='user.html' id="+data.userid+">"+"欢迎-"+data.username+"</a>";
		$(node).appendTo(".rightTopBar");
	},"json");
};

function getUrlItemId() {
	var urlstr=window.location.search;
	var arr= new Array();
	arr=urlstr.split("=");
	return arr[1];
};

function getThisShopMsg(itemId) {
	$.post("/CuitShopPlatform/servlet/DetailController", {"action":"getThisShop","itemId":itemId},function(data){
		var price=data[0].price;
		price="￥"+price;
		var shopname=data[0].name;
		var shopImg=data[0].picture;
		var information=data[0].information;
		var shopId=data[0].id;
		$(".shopname").text(shopname);
		$(".shopname").attr("id",shopId);
		$(".price").text(price);
		$(".shopImg>img").attr("src",shopImg);
	},"json");
};

function add(){
	var thisNum=$(".Num").val();
	thisNum++;
	$(".Num").val(thisNum);
};

function sub(){
	var thisNum=$(".Num").val();
	if(thisNum==1)
		return;
	thisNum--;
	$(".Num").val(thisNum);
};

function addCart(itemId,shopNum,picture,shopname,userid,price){
	$.post("/CuitShopPlatform/servlet/DetailController",{"action":"addCart","itemId":itemId,"shopNum":shopNum,"picture":picture,"shopname":shopname,"userId":userid,"price":price},function(data){
		if (data.flag) {
			alert("添加成功");
		}
	})
};
$(function(){
	isLogin();
	var itemId=getUrlItemId();
	$(".Num").val(1);
	 getThisShopMsg(itemId);
	 $(document).on("click",".add", function(){
	 	add();
	 });
	 $(document).on("click",".sub",function(){
	 	sub();
	 });
	 $(document).on("click",".addcart",function(){
			$.post("/CuitShopPlatform/servlet/isLoginController",{"action":"isLogin"}, function(data){
				if(data.loginflag=='0'){
					alert("请先登录");
					window.location.href="login.html";
				}
				$(".rightTopBar").empty();
				var node="<a class='userMsg' href='user.html' id="+data.userid+">"+"欢迎-"+data.username+"</a>";
				$(node).appendTo(".rightTopBar");
				userid=parseInt($(".userMsg").attr("id"));
				console.log(userid);
			 	var itemId=$(".shopname").attr("id");
			 	var price=$(".price").text().replace(/[^0-9]/ig,"");
			 	var shopNum=$(".Num").val();
			 	var picture=$(".shopImg img").attr("src");
			 	var shopname=$(".shopname").text();
			 	addCart(itemId,shopNum,picture,shopname,userid,price);
				
			},"json");

	 });
})